package com.alibaba.springbootrbac.controller;

import com.alibaba.springbootrbac.model.param.user.*;
import com.alibaba.springbootrbac.model.response.user.UserInfoVO;
import com.alibaba.springbootrbac.model.response.user.UserLoginVO;
import com.alibaba.springbootrbac.model.response.user.UserRegisterVO;
import com.alibaba.springbootrbac.service.UserService;
import com.alibaba.springbootrbac.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouYaDong
 * @date 2025/8/21
 * @description 用户管理控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private  UserService userService;

    @PostMapping("/register")
    public Result<UserRegisterVO> register(@RequestBody @Validated UserRegisterParam param) {
       return Result.success(userService.register(param));
    }

    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody @Validated UserLoginParam param){
        return Result.success(userService.login(param));
    }

    // 用户注销
    @PostMapping("/logout")
    public Result<String> logout(){
        // 这里后端不做任何逻辑处理，让前端自己跟根据回调结果删除本地token即可
       return Result.success("用户注销成功");
    }

    @PreAuthorize("hasAuthority('USER_DELETE')")
    @PostMapping("/deleteUserById")
    public Result<String> deleteUserById(@RequestBody @Validated DeleteUserParam param){
        userService.deleteUserById(param.getId());
        return Result.success("删除成功，id"+param.getId());
    }

    @PreAuthorize("hasAuthority('USER_DELETE')")
    @PostMapping("/deleteUserBatch")
    public Result<String> deleteUserBatch(@RequestBody @Validated DeleteUserBatchParam param){
        userService.deleteUserByIds(param.getIds());
        return Result.success("批量删除成功");
    }

    @PreAuthorize("hasAuthority('USER_UPDATE')")
    @PostMapping("/updateUserById")
    public Result<String> updateUserById(
            @RequestBody @Validated UpdateUserParam param){
        userService.updateUserById(param);
        return Result.success("更新成功");
    }

    @PreAuthorize("hasAuthority('USER_QUERY')")
    @GetMapping("/getUserInfoById")
    public Result<UserInfoVO> getUserInfoById(@RequestParam("id") String id){
        return Result.success(userService.getUserInfoById(id));
    }

    @PreAuthorize("hasAuthority('USER_LIST_QUERY')")
    @PostMapping("/getUsersInfoByPage")
    public Result<IPage<UserInfoVO>> getUsersInfoByPage(
            @RequestBody @Validated UserPageQueryParam param){
        return Result.success(userService.getUsersInfoByPage(param));
    }

    @PostMapping("/assignRoles")
    public Result<String> assignRoles(@RequestBody @Validated UserRoleParam param){
        userService.assignRolesToUser(param);
        return Result.success("分配角色成功");
    }

    @PostMapping("/removeUserRoles")
    public Result<String> removeUserRoles(@RequestBody @Validated UserRoleParam param){
        userService.removeUserRoles(param);
        return Result.success("移除用户角色成功");
    }
}
